﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace T3Task1
{
    class DBHelper
    {
        //静态字段
        public static string connStr = "server.;database=Student;Integrated Security=true";

        //静态方法一查询
        public static SqlDataReader Select(string sql)
        {
            //1、创建连接对象
            SqlConnection conn = new SqlConnection(connStr);
            //2、打开连接
            conn.Open();
            //3、创建对象命令
            SqlCommand cmd = new SqlCommand(sql, conn);
            //4、ExecuteReader 查询
            return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

        }
        //静态方法二增删改
        public static bool GetNonQuery(string sql)
        {
            bool result = false;
            //1、创建连接对象
            SqlConnection conn = new SqlConnection(connStr);
            //2、打开连接
            conn.Open();
            //3、创建对象命令
            SqlCommand cmd = new SqlCommand(sql, conn);
            //4、ExecuteNonQuery 增删改
            int number = cmd.ExecuteNonQuery();
            if (number > 0)
            {
                result = true;
            }
            return result;

        }
        public static void Scalar(string sql)
        {
            //1、创建连接对象
            SqlConnection conn = new SqlConnection(connStr);
            //2、打开连接
            conn.Open();

            string countSql = "select count(*) from gamelist";
            //3、创建对象命令
            SqlCommand countcmd = new SqlCommand(countSql, conn);
            //4、ExecuteNonQuery 统计
            string count = countcmd.ExecuteScalar().ToString();
            Console.WriteLine("共有{0}款游戏",count);
        }
    }
}
